<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师信息</title>
    <style>
        #container {
            width: 80%;
            margin: 10px auto;
        }
        .teacher {
            width: 100%;
            margin: 0 auto;
            padding: 10px 0;
            border-bottom: 1px dashed gray;
            overflow: auto;
        }
        .teacher div {
            float: left;
        }
        .photo {
            height: 140px;
            border-radius: 75px;
            overflow: hidden;
            margin-left: 20px;
        }
        .info {
            width: 75%;
            margin-left: 30px;
        }
        .info div {
            clear: both;
            margin: 5px 10px;
        }
        .info span {
            margin-right: 25px;
        }
        .info a {
            text-decoration: none;
            color: darkcyan;
        }
        .user {
            float: right;
            margin: 10px 10px;
        }
        .user+h1 {
            clear: both;
        }
        #photo {
            border-radius: 32px 32px 32px 32px;
        }
        .comment>a:hover {
            text-shadow: 3px 3px 3px orangered;
        }
    </style>
</head>
<body>
    <div id="container">
        <div class="user">
            {% if request.session.userid %}
                {{ request.session.username }}
                <a href="/logout/">注销</a>
            {% else %}
                <a href="/login/">用户登录</a>
            {% endif %}
            <a href=/register/>快速注册</a>
        </div>
        <h1>{{ subject.name }}学科的老师信息</h1>
        <hr>
        {% if not teachers %}
            <h2>暂无该学科老师信息</h2>
        {% endif %}

        {% for teacher in teachers %}
        <div class="teacher">
            <div class="photo">
                <img src="/static/images/{{ teacher.photo }}" height="140" alt="">
            </div>
            <div class="info">
                <div>
                    <span><strong>姓名：{{ teacher.name }}</strong></span>
                    <span><strong>性别：{{ teacher.sex | yesno:'男,女' }}</strong></span>
                    <span><strong>出生日期：{{ teacher.birth }}</strong></span>
                </div>
                <div class="intro">
                    {{ teacher.intro }}
                </div>
                <div class="comment">
                    <a href="/praise/?tno={{ teacher.no }}">好评：</a>&nbsp;&nbsp;(<strong>{{ teacher.good_count }}</strong>)
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="/criticize/?tno={{ teacher.no }}">差评：</a>&nbsp;&nbsp;(<strong>{{ teacher.bad_count }}</strong>)
                </div>
            </div>
        </div>
        {% endfor %}
        <a href="/">返回首页</a>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>

        $('.comment>a').on('click', (evt) => {
            evt.preventDefault()
            let url = $(evt.target).attr('href')
            $.getJSON(url, (json) => {
                if (json.code === 20000) {
                        $(evt.target).next().text(json.count)
                    }  else {
                    alert(json.message)
                    if (json.code === 20002) {
                        // 把字符串处理成百分号编码
                        let url = encodeURIComponent(location.pathname + location.search)
                        location.href = '/login/?backurl=' + url
                    }
                }
            })
        })
    </script>

{#    <script>#}
{#        // 拿到两个a标签#}
{#        let anchors = document.querySelectorAll('.comment>a')#}
{#        // 通过循环为a标签绑定点击事件#}
{#        for(let i = 0; i < anchors.length; ++i) {#}
{#            // 添加事件监听器（绑定事件回调函数）#}
{#            anchors[i].addEventListener('click',(evt) => {#}
{#                // 阻止事件默认行为（点击a标签不刷新页面）#}
{#                evt.preventDefault()#}
{#                // 获取异步请求的url地址#}
{#                let url = evt.target.getAttribute('href')#}
{#                // 想服务器发起异步请求 Promise对象#}
{#                fetch(url).then(resp =>  resp.json()).then(json => {#}
{#                    // 获取到服务器的json格式的数据， 对页面进行局部刷新#}
{#                    if (json.code === 20000) {#}
{#                        // 获取strong标签#}
{#                        let strong = evt.target.nextElementSibling#}
{#                        strong.textContent = json.count#}
{#                    } else {#}
{#                        alert(json.message)#}
{#                    }#}
{#                })#}
{#            })#}
{#        }#}
{#    </script>#}
</body>
</html>